<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${baseInfo.mybatisNamespace}">	
	
	<sql id="column_sql">
		<#list baseInfo.propertyInfos as pi>
		<#if pi_index != 0>
		, ${baseInfo.tableName}.${pi.columnName!''}
		<#else>
		${baseInfo.tableName}.${pi.columnName!''}
		</#if>
		</#list>
	</sql>
	
	<sql id="column_sql_nokey">
		<#list baseInfo.propertyInfosNoKey as pi>
		<#if pi_index != 0>
		, ${pi.columnName!''}
		<#else>
		${pi.columnName!''}
		</#if>
		</#list>
	</sql>
	
	<sql id="whereClause_sql">
		<#list baseInfo.propertyInfos as pi>
		<if test="${pi.propertyName!''} != null"> and ${baseInfo.tableName}.${pi.columnName!''} = ${r"#{"}${pi.propertyName!''},jdbcType=${pi.jdbcType!''}${r"}"}</if>
		</#list>
	</sql>
	
	
	<resultMap id="${baseInfo.className}Result" type="${wholeClassName}">
		<#list baseInfo.propertyInfos as pi>
		<result column="${pi.columnName!''}" property="${pi.propertyName!''}"/>
		</#list>
		<#list baseInfo.listSelectPropertyInfo as pi>
		<result column="${pi.columnName!''}_NAME" property="${pi.propertyName!''}Name"/>
		</#list>
	</resultMap>
	
	<insert id="insert" parameterType="${wholeClassName}" keyProperty="${baseInfo.primaryKeyProperty}">
		insert into ${baseInfo.tableName}(
				<trim prefixOverrides=",">
				<#list baseInfo.propertyInfosNoKey as pi>
				<if test="${pi.propertyName!''} != null">,${pi.columnName!''}</if>
				</#list>
				</trim>
		 	 	 )
		 values (
		 		<trim prefixOverrides=",">
		 		<#list baseInfo.propertyInfosNoKey as pi>
				<if test="${pi.propertyName!''} != null"> ,${r"#{"}${pi.propertyName!''},jdbcType=${pi.jdbcType!''}${r"}"}</if>
				</#list>
				</trim>
		 	 	 )
	</insert>
	
	
	<update id="updateById" parameterType="${wholeClassName}">
		update ${baseInfo.tableName}  
	<set>
		<trim suffixOverrides=",">
		<#list baseInfo.propertyInfosNoKey as pi>
		<if test="${pi.propertyName!''} != null">${pi.columnName!''} = ${r"#{"}${pi.propertyName!''},jdbcType=${pi.jdbcType!''}${r"}"},</if>
		</#list>
		</trim>
	</set>
		where ${baseInfo.primaryKey} = ${r"#{"}${baseInfo.primaryKeyProperty},jdbcType=BIGINT${r"}"}
	</update>
	
	<update id="update" parameterType="${wholeClassName}">
		update ${baseInfo.tableName}  
	<set>
		<trim suffixOverrides=",">
		<#list baseInfo.propertyInfosNoKey as pi>
		<if test="${pi.propertyName!''} != null">${pi.columnName!''} = ${r"#{"}${pi.propertyName!''},jdbcType=${pi.jdbcType!''}${r"}"},</if>
		</#list>
		</trim>
	</set>
	<where>
		<include refid="whereClause_sql"/>
	</where>
	</update>

	<insert id="insertBatch" parameterType="list" useGeneratedKeys="true">
		insert into ${baseInfo.tableName} (<include refid="column_sql_nokey"/>) values
		<foreach collection="list" item="entity" index="index" separator=",">
				( 
				<trim suffixOverrides=",">
				<#list baseInfo.propertyInfosNoKey as pi>
				${r"#{"}entity.${pi.propertyName!''},jdbcType = ${pi.jdbcType!''}${r"}"},
				</#list>
				</trim>
				)
		</foreach>
	</insert>
	
	<delete id="delete" parameterType="${wholeClassName}">
	    delete from ${baseInfo.tableName}  where  ${baseInfo.primaryKey} = ${r"#{"}${baseInfo.primaryKeyProperty},jdbcType=BIGINT${r"}"}
	</delete>
	
	<delete id="deleteIds"  parameterType="list">
		delete from ${baseInfo.tableName}  where
		<foreach collection="list" item="entity" index="index" separator="," open="${baseInfo.primaryKey} in (" close=")">
			${r"#{"}entity,jdbcType=BIGINT${r"}"}
		</foreach>
	</delete>
	 
	<select id="count" parameterType="${wholeClassName}" resultType="int">
		select count(*) from ${baseInfo.tableName}  
			 <where>
				<include refid="whereClause_sql"/>
			</where>
	</select>
	 
	<select id="select" parameterType="${wholeClassName}" resultMap="${baseInfo.className}Result">
		select 
			<include refid="column_sql"/>
			<#list baseInfo.listSelectPropertyInfo as pi>
			,(select st.dict_name from td_sm_dict st where st.parent_code = '${pi.dictParentCode!''}' and st.dict_code = ${baseInfo.tableName}.${pi.columnName!''} ) ${pi.columnName!''}_NAME
			</#list>
		    from ${baseInfo.tableName} 
			 <where>
				<include refid="whereClause_sql"/>
			</where>
	</select>
	
	<select id="selectByPage" parameterType="map" resultMap="${baseInfo.className}Result">
		select 
			<include refid="column_sql"/>
			<#list baseInfo.listSelectPropertyInfo as pi>
			,(select st.dict_name from td_sm_dict st where st.parent_code = '${pi.dictParentCode!''}' and st.dict_code = ${baseInfo.tableName}.${pi.columnName!''} ) ${pi.columnName!''}_NAME
			</#list>
		    from ${baseInfo.tableName} 
		    <where>
		    	<#list baseInfo.propertyInfos as pi>
				<#if pi.searchType??>
				<if test="${pi.propertyName!''} != null"> and ${baseInfo.tableName}.${pi.columnName!''}${r" like CONCAT(CONCAT('%', #{"}${pi.propertyName!''}${r"}),'%')"}</if>
				<#else>
				<if test="${pi.propertyName!''} != null"> and ${baseInfo.tableName}.${pi.columnName!''} = ${r"#{"}${pi.propertyName!''},jdbcType=${pi.jdbcType!''}${r"}"}</if>
				</#if>
				</#list>
		    </where>
		    order by ${baseInfo.primaryKey} desc
			limit ${r"#{"}startRow,jdbcType=INTEGER${r"}"},${r"#{"}pageSize,jdbcType=INTEGER${r"}"}
	</select>
	
	<select id="selectByPageTotalSize" parameterType="map" resultType="Integer">
		select count(*) from ${baseInfo.tableName} 
		<where>
	    	<#list baseInfo.propertyInfos as pi>
				<#if pi.searchType??>
				<if test="${pi.propertyName!''} != null"> and ${baseInfo.tableName}.${pi.columnName!''}${r" like CONCAT(CONCAT('%', #{"}${pi.propertyName!''}${r"}),'%')"}</if>
				<#else>
				<if test="${pi.propertyName!''} != null"> and ${baseInfo.tableName}.${pi.columnName!''} = ${r"#{"}${pi.propertyName!''},jdbcType=${pi.jdbcType!''}${r"}"}</if>
				</#if>
			</#list>
		</where>
	</select>
	
	<select id="selectPageAll" parameterType="map" resultMap="${baseInfo.className}Result">
		select 
			<include refid="column_sql"/>
			<#list baseInfo.listSelectPropertyInfo as pi>
			,(select st.dict_name from td_sm_dict st where st.parent_code = '${pi.dictParentCode!''}' and st.dict_code = ${baseInfo.tableName}.${pi.columnName!''} ) ${pi.columnName!''}_NAME
			</#list>
			from ${baseInfo.tableName} 
			<where>
	    	<#list baseInfo.propertyInfos as pi>
				<#if pi.searchType??>
				<if test="${pi.propertyName!''} != null"> and ${baseInfo.tableName}.${pi.columnName!''}${r" like CONCAT(CONCAT('%', #{"}${pi.propertyName!''}${r"}),'%')"}</if>
				<#else>
				<if test="${pi.propertyName!''} != null"> and ${baseInfo.tableName}.${pi.columnName!''} = ${r"#{"}${pi.propertyName!''},jdbcType=${pi.jdbcType!''}${r"}"}</if>
				</#if>
			</#list>
		</where>
		    order by ${baseInfo.primaryKey} desc
	</select>
	
	<select id="selectAll" resultMap="${baseInfo.className}Result">
		select 
			<include refid="column_sql"/>
			<#list baseInfo.listSelectPropertyInfo as pi>
			,(select st.dict_name from td_sm_dict st where st.parent_code = '${pi.dictParentCode!''}' and st.dict_code = ${baseInfo.tableName}.${pi.columnName!''} ) ${pi.columnName!''}_NAME
			</#list>
		    from ${baseInfo.tableName} 
	</select>
	
	<select id="selectByID" parameterType="java.lang.Long" resultMap="${baseInfo.className}Result">
		select 
			<include refid="column_sql"/>
			<#list baseInfo.listSelectPropertyInfo as pi>
			,(select st.dict_name from td_sm_dict st where st.parent_code = '${pi.dictParentCode!''}' and st.dict_code = ${baseInfo.tableName}.${pi.columnName!''} ) ${pi.columnName!''}_NAME
			</#list>
		    from ${baseInfo.tableName} 
			 where ${baseInfo.tableName}.${baseInfo.primaryKey} = ${r"#{"}${baseInfo.primaryKeyProperty},jdbcType=BIGINT${r"}"}
	</select>
	
	<delete id="deleteByProperty" parameterType="${wholeClassName}">
		delete from ${baseInfo.tableName} 
		<where>
			<include refid="whereClause_sql"/>
		</where> 
	</delete>

</mapper>